<template>
    <div class="user-list">
      <el-card>
        <div slot="header">
          <el-form ref="form" :inline="true" :model="form" label-width="80px">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone"></el-input>
            </el-form-item>
            <el-form-item label="注册时间" prop="rangeDate">
              <el-date-picker
                v-model="form.rangeDate"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button  @click="onReset">重置</el-button>
              <el-button type="primary" @click="onSubmit" :disabled="isLoading">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="id"
            label="用户ID">
          </el-table-column>
          <el-table-column
            prop="portrait"
            label="头像">
            <template slot-scope="scope">
              <img width="30px" :src="scope.row.portrait || 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'" alt="">
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="用户名">
          </el-table-column>
          <el-table-column
            prop="phone"
            label="手机号">
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="注册时间">
          </el-table-column>
          <el-table-column
            prop="isDel"
            label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="ENABLE"
                inactive-value="DISABLE"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="handleForbidUser(scope.row)">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleSelectRole(scope.row)">分配角色</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog
          title="分配角色"
          :visible.sync="dialogVisible"
          width="50%"
          :before-close="handleClose">
          <!-- <span>这是一段信息</span> -->
          <el-select v-model="roleIdList" multiple placeholder="请选择">
            <el-option
              v-for="item in roles"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleAllocRole">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
</template>

<script>
import { getAllRoles, allocateUserRoles, getUserRoles } from '@/services/role'
import { getUserPages, forbidUser } from '@/services/user'
export default {
  name: 'UserList',
  data () {
    return {
      form: {
        currentPage: 1,
        pageSize: 100,
        phone: '',
        startCreateTime: '',
        endCreateTime: '',
        rangeDate: []
      },
      tableData: [],
      isLoading: true,
      dialogVisible: false,
      roles: [],
      roleIdList: [],
      currentRoleId: null
    }
  },
  created () {
    this.loadAllUser()
  },
  methods: {
    async handleAllocRole () {
      const { data } = await allocateUserRoles({ userId: this.currentRoleId, roleIdList: this.roleIdList })
      if (data.code === '000000') {
        this.$message.success('分配成功')
        this.dialogVisible = false
      }
    },
    async handleSelectRole (role) {
      this.dialogVisible = true
      this.currentRoleId = role.id
      const { data } = await getAllRoles()
      if (data.code === '000000') {
        this.roles = data.data
      }

      const { data: data2 } = await getUserRoles(role.id)
      if (data2.code === '000000') {
        this.roleIdList = data2.data.map(item => item.id)
      }
    },
    onReset () {
      this.$refs.form.resetFields()
      this.loadAllUser()
    },
    onSubmit () {
      this.form.currentPage = 1
      this.loadAllUser()
    },
    async handleForbidUser (user) {
      const { data } = await forbidUser(user.id)
      if (data.code === '000000') {
      }
    },
    async loadAllUser () {
      this.isLoading = true
      const { rangeDate } = this.form
      if (rangeDate && rangeDate.length) {
        this.form.startCreateTime = rangeDate[0]
        this.form.endCreateTime = rangeDate[1]
      } else {
        this.form.startCreateTime = ''
        this.form.endCreateTime = ''
      }
      const { data } = await getUserPages(this.form)
      if (data.code === '000000') {
        this.tableData = data.data.records
        this.isLoading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped ></style>
